package cn.songit.util;

import cn.songit.entity.ExcelDemoData;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelReadTest {

    private final static Logger logger = LoggerFactory.getLogger("ExcelReadTest.class");

    /**
     * 最简单的读
     * <p>
     * 1. 创建excel对应的实体对象 参照{@link ExcelDemoData}
     * <p>
     * 2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照{@link ExcelDemoDataListener}
     * <p>
     * 3. 直接读即可
     */
    @Test
    public void simpleRead() {
        // 写法1：JDK8+ ,不用额外写一个ExcelDemoDataListener
        // since: 3.0.0-beta1
        String fileName = "E:/JavaDemo/file/slow_sql.xlsx";
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
        List<String> sql_gauss_list = new ArrayList<>();
        List<String> sql_dts_list = new ArrayList<>();
        Map<String,String> map = new HashMap<>();
        EasyExcel.read(fileName, ExcelDemoData.class, new PageReadListener<ExcelDemoData>(dataList -> {
            for (ExcelDemoData excelDemoData : dataList) {
                //logger.info("读取到一条数据{}", JSON.toJSONString(excelDemoData));
                sql_gauss_list.add(excelDemoData.getSql_gauss().trim().replace(" ", ""));
                if (excelDemoData.getSql_dts() != null ){
                    sql_dts_list.add(excelDemoData.getSql_dts().trim().replace(" ", ""));
                }
            }
        })).sheet().doRead();
        for (int i = 0; i < sql_gauss_list.size(); i++) {
            for (String sql_dts : sql_dts_list) {
                if (sql_dts.contains(sql_gauss_list.get(i))){
                    map.put(sql_gauss_list.get(i),sql_dts);
                    logger.info(sql_gauss_list.get(i));
                    logger.info(sql_dts);
                }
            }
        }
        //logger.info(JSON.toJSONString(map));
    }
}